﻿CREATE PROCEDURE [dbo].[SupplierInvoiceLinesInsert]
	@DocId [int],
	@GoodsId [int],
	@BarCode [char](12),
	@DocQntty [int],
	@Qntty [int],
	@SupplierGoodsId [int],
	@ManufacturerPrice [decimal](19, 2),
	@SupplierPrice [decimal](19, 2),
	@SupplierSumNOTAX [decimal](19, 2),
	@ImportMarkup [float],
	@SupplierMarkup [float],
	@BatchNumber [varchar](20),
	@ReadyBefore [smalldatetime],
	@MadeDate [smalldatetime],
	@NDSTax [tinyint],
	@NDSValue [decimal](19, 2),
	@ReestrPrice [decimal](19, 2),
	@ReestrDate [smalldatetime],
	@GTDNumber [varchar](30),
	@CertificateNumber [varchar](80),
	@CertificateDate [smalldatetime],
	@CertificateOrgId [int],
	@RetailMarkup [float],
	@RetailPrice [decimal](19, 2),
	@Note [varchar](250),
	@MD [datetime],
	@MW [varchar](50)
WITH EXECUTE AS CALLER
AS
DECLARE @Id int, @intInputLineId int, @intDocTypeId int 
DECLARE @intContragentId int, @datContragentDocDate smalldatetime, @strContragentDocNumber varchar(20)
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int, @intRowCount int

SET NOCOUNT ON
IF (@GoodsId Is Null) OR (@DocQntty Is Null) OR (@SupplierGoodsId Is Null) OR (@DocId Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @intContragentId = ContragentId, @datContragentDocDate = ContragentDocDate,
@strContragentDocNumber = ContragentDocNumber, @intDocTypeId = DocTypeId 
FROM CA.Documents WHERE Id = @DocId

IF (@intDocTypeId <> 2)
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Неправильный тип документа!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
--получение значения идентификатора
EXEC @intRet = dbo.GetNextCounterFor 'GoodsLines', @intInputLineId OUTPUT
IF @intRet <> 0 RETURN @intRet

IF (@Qntty Is NULL) OR (@Qntty < 1) --нет фактического количества по накладной
BEGIN --проводки по приходной накладной не создаются
	SET NOCOUNT OFF;

	EXEC @intRet = dbo.InputLinesInsertUpdate @DocId,@GoodsId,@DocQntty,@SupplierGoodsId,@ManufacturerPrice,
	@SupplierPrice,@SupplierSumNOTAX,@ImportMarkup,@SupplierMarkup,@BatchNumber,@ReadyBefore,@MadeDate,
	@NDSTax,@NDSValue,@ReestrPrice,@ReestrDate,@GTDNumber,
	@CertificateNumber,@CertificateDate,@CertificateOrgId,@RetailMarkup,
	@intContragentId,@datContragentDocDate,@strContragentDocNumber,
	@Note,@MD,@MW,
	@intInputLineId, 0, @intRowCount OUTPUT
	IF @intRet <> 0 RETURN @intRet

END ELSE BEGIN

	BEGIN TRAN

	EXEC @intRet = dbo.InputLinesInsertUpdate @DocId,@GoodsId,@DocQntty,@SupplierGoodsId,@ManufacturerPrice,
	@SupplierPrice,@SupplierSumNOTAX,@ImportMarkup,@SupplierMarkup,@BatchNumber,@ReadyBefore,@MadeDate,
	@NDSTax,@NDSValue,@ReestrPrice,@ReestrDate,@GTDNumber,
	@CertificateNumber,@CertificateDate,@CertificateOrgId,@RetailMarkup,
	@intContragentId,@datContragentDocDate,@strContragentDocNumber,
	@Note,@MD,@MW,
	@intInputLineId, 1, @intRowCount OUTPUT
	IF @intRet <> 0 RETURN @intRet

	EXEC @intRet = dbo.PartyGoodsInsert	@intInputLineId, @DocId, @GoodsId,
	@BarCode, @Qntty, @RetailPrice, Null,
	@Note, @MD, @MW, 0
	IF @intRet <> 0 RETURN @intRet

	COMMIT TRAN
END

SELECT Id, DocId, GoodsId, BarCode, DocQntty, Qntty, RetailSum, SupplierGoodsId, ManufacturerPrice, 
SupplierPrice, SupplierSumNOTAX, ImportMarkup, SupplierMarkup, BatchNumber, ReadyBefore, MadeDate, NDSTax, 
NDSValue, ReestrPrice, ReestrDate, GTDNumber, CertificateNumber, CertificateDate, 
CertificateOrgId, RetailMarkup, RetailPrice, Note, MD, MW 
FROM dbo.vwInputLines WHERE (Id = @intInputLineId)

RETURN 0


